package com.android.publicModule.domain;

import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.publicModule.network.HttpRequest;
import com.android.publicModule.system.SystemInfo;
import com.android.publicModule.tools.Tools;

import android.content.Context;

public class ZZDomainControl {

	private static final String HTTP = "http";
	private static final String HTTP_HEAD = "http://";
	private static final String PING_RELATIVE_URL = "/";//PING的相对地址
	private static final String CONFIGE_RELATIVE_URL = "/domainConfig.jsp";//拉取配置的相对地址///yyVoiceServer/domainConfig.jsp
	private static final String[] CONFIGE_DOMAIN = {"http://c.kmobi.net","http://c.jj06.net"};//YY主备域名列表    //"http://192.168.1.16","http://192.168.1.14","http://192.168.1.123:8080"
	private static final String[] YY_DOMAIN = {"http://c.kmobi.net","http://y.uu08.net"};//YY主备域名列表   //"http://www.yyvoice.net"  "http://c.kmobi.net","http://y.uu08.net"
	//"http://fuzywujie.vicp.cc:8088/yyVoiceServer","http://192.168.1.54:8080/yyVoiceServer","http://c.kmobi.net","http://y.uu08.net"
	private static final String[] PUSH_DOMAIN = {"http://p.kmobi.net","http://p.oo66.net"};//PUSH主备域名列表  //"http://www.iadmob.com" "http://p.ymobi.net","http://p.oo66.net"
	private String yyDomain;//当前默认YY域名
	private String pushDomain;//当前默认PUSH域名
	private String[] yyDomainArr;//当前YY域名列表
	private String[] pushDomainArr;//当前PUSH域名列表
	private String[] configeDomainArr;
	private static final String DB_NAME = "zzDomain";
	private static final String split = "+";
	private static final int DOMAIN_TYPE_YY = 1;//YY的域名类型标识
	private static final int DOMAIN_TYPE_PUSH = 2;//PUSH的域名类型标识
	private static ZZDomainControl zzDomainControl;
	private HttpRequest httpRequest;
	/**
	 * 构造方法
	 */
	private ZZDomainControl() {
	}
	
	/**
	 * 获取域名控制类单例模式
	 * @return
	 */
	public static ZZDomainControl getInstance() {
		if(zzDomainControl==null)
		{
			zzDomainControl = new ZZDomainControl();
		}
		return zzDomainControl;
	}
	
	public void updateAndCheckDomain(Context context) {
		show(context,"begin downloadNewDomain");
		downloadNewDomain(context);
		show(context,"begin checkDomainAvailable");
		checkDomainAvailable(context);
	}
	
	private void show(Context context,String end) {
		String[] configeArr = getConfigeDomainArr(context);
		String[] yyArr = getYYDomainArr(context);
		String[] pushArr = getPushDomainArr(context);
		String yy = getYYDomain(context);
		String push = getPushDomain(context);
		
		if(configeArr!=null&&configeArr.length>0)
		{
			for(int i=0;i<configeArr.length;i++) {
				Tools.showLog("ZZDomainControl-show"+end, "configeArr["+i+"]="+configeArr[i]);
			}
		}
		else
		{
			Tools.showLog("ZZDomainControl-show"+end, "configeArr==null||configeArr.length==0");
		}
		
		if(yyArr!=null&&yyArr.length>0)
		{
			for(int i=0;i<yyArr.length;i++) {
				Tools.showLog("ZZDomainControl-show"+end, "yyArr["+i+"]="+yyArr[i]);
			}
		}
		else
		{
			Tools.showLog("ZZDomainControl-show"+end, "yyArr==null||yyArr.length==0");
		}
		
		if(pushArr!=null&&pushArr.length>0)
		{
			for(int i=0;i<pushArr.length;i++) {
				Tools.showLog("ZZDomainControl-show"+end, "pushArr["+i+"]="+pushArr[i]);
			}
		}
		else
		{
			Tools.showLog("ZZDomainControl-show"+end, "pushArr==null||pushArr.length==0");
		}
		
		Tools.showLog("ZZDomainControl-show"+end, "yy="+yy);
		Tools.showLog("ZZDomainControl-show"+end, "push="+push);
	}
	
	/**
	 * 获取YY语音的域名
	 * @param context
	 * @return
	 */
	public String getYYDomain(Context context) {
		if(yyDomain!=null&&yyDomain.length()>0)
		{
			Tools.showLog("ZZDomainControl-getYYDomain", "currentYYDomain is "+yyDomain);
			return yyDomain;
		}
		if(context!=null)
		{
			yyDomain = Tools.getSaveData(context, DB_NAME, "yyDomain", null);
			Tools.showLog("ZZDomainControl-getYYDomain", "yyDomain="+yyDomain);
		}
		if(yyDomain==null||yyDomain.length()==0)
		{
			Tools.showLog("ZZDomainControl-getYYDomain", "yyDomain==null||yyDomain.length()==0");
			yyDomain = getYYDomainArr(context)[0];
		}
		Tools.showLog("ZZDomainControl-getYYDomain", "currentYYDomain is "+yyDomain);
		return yyDomain;
	}
	
	/**
	 * 获取PUSH域名
	 * @param context
	 * @return
	 */
	public String getPushDomain(Context context) {
		if(pushDomain!=null&&pushDomain.length()>0)
		{
			Tools.showLog("ZZDomainControl-getPushDomain", "currentPushDomain is "+pushDomain);
			return pushDomain;
		}
		if(context!=null)
		{
			pushDomain = Tools.getSaveData(context, DB_NAME, "pushDomain", null);
			Tools.showLog("ZZDomainControl-getPushDomain", "pushDomain="+pushDomain);
		}
		if(pushDomain==null||pushDomain.length()==0)
		{
			Tools.showLog("ZZDomainControl-getPushDomain", "pushDomain==null||pushDomain.length()==0");
			pushDomain = getPushDomainArr(context)[0];
		}
		Tools.showLog("ZZDomainControl-getPushDomain", "currentPushDomain is "+pushDomain);
		return pushDomain;
	}
	
	/**
	 * 更新YY域名
	 * @param context
	 * @param domain
	 */
	private void updateYYDomain(Context context,String domain) {
		Tools.showLog("ZZDomainControl-updateYYDomain", "domain="+domain);
		if(domain!=null&&domain.length()>0)
		{
			yyDomain = domain;
			if(context!=null)
			{
				 Tools.saveData(context, DB_NAME, "yyDomain", domain);
			}
		}
	}
	
	/**
	 * 更新PUSH域名
	 * @param context
	 * @param domain
	 */
	private void updatePushDomain(Context context,String domain) {
		Tools.showLog("ZZDomainControl-updatePushDomain", "domain="+domain);
		if(domain!=null&&domain.length()>0)
		{
			pushDomain = domain;
			if(context!=null)
			{
				Tools.saveData(context, DB_NAME, "pushDomain", domain);
			}
		}
	}
	
	/**
	 * 获取当前配置域名列表
	 * @param context
	 * @return
	 */
	private String[] getConfigeDomainArr(Context context) {
		if(configeDomainArr!=null&&configeDomainArr.length>0)
		{
			return configeDomainArr;
		}
		if(context!=null)
		{
			String configeDomainArrStr = Tools.getSaveData(context, DB_NAME, "configeDomainArrStr", null);
			if(configeDomainArrStr!=null&&configeDomainArrStr.length()>0)
			{
				ArrayList<String> list = Tools.splitStr(configeDomainArrStr, split);
				if(list!=null&&list.size()>0)
				{
					configeDomainArr = Tools.arrayListToArr(list);
				}
			}
			Tools.showLog("ZZDomainControl-getConfigeDomainArr", "configeDomainArrStr="+configeDomainArrStr);
		}
		if(configeDomainArr==null||configeDomainArr.length==0)
		{
			Tools.showLog("ZZDomainControl-getConfigeDomainArr", "configeDomainArr==null||configeDomainArr.length==0");
			configeDomainArr = CONFIGE_DOMAIN;
		}
		return configeDomainArr;
	}
	
	/**
	 * 获取当前YY域名列表
	 * @param context
	 * @return
	 */
	private String[] getYYDomainArr(Context context) {
		if(yyDomainArr!=null&&yyDomainArr.length>0)
		{
			return yyDomainArr;
		}
		if(context!=null)
		{
			String yyDomainArrStr = Tools.getSaveData(context, DB_NAME, "yyDomainArrStr", null);
			if(yyDomainArrStr!=null&&yyDomainArrStr.length()>0)
			{
				ArrayList<String> list = Tools.splitStr(yyDomainArrStr, split);
				if(list!=null&&list.size()>0)
				{
					yyDomainArr = Tools.arrayListToArr(list);
				}
			}
			Tools.showLog("ZZDomainControl-getYYDomainArr", "yyDomainArrStr="+yyDomainArrStr);
		}
		if(yyDomainArr==null||yyDomainArr.length==0)
		{
			Tools.showLog("ZZDomainControl-getYYDomainArr", "yyDomainArr==null||yyDomainArr.length==0");
			yyDomainArr = YY_DOMAIN;
		}
		return yyDomainArr;
	}
	
	/**
	 * 获取当前PUSH域名列表
	 * @param context
	 * @return
	 */
	private String[] getPushDomainArr(Context context) {
		if(pushDomainArr!=null&&pushDomainArr.length>0)
		{
			return pushDomainArr;
		}
		if(context!=null)
		{
			String pushDomainArrStr = Tools.getSaveData(context, DB_NAME, "pushDomainArrStr", null);
			if(pushDomainArrStr!=null&&pushDomainArrStr.length()>0)
			{
				ArrayList<String> list = Tools.splitStr(pushDomainArrStr, split);
				if(list!=null&&list.size()>0)
				{
					pushDomainArr = Tools.arrayListToArr(list);
				}
			}
			Tools.showLog("ZZDomainControl-getPushDomainArr", "pushDomainArrStr="+pushDomainArrStr);
		}
		if(pushDomainArr==null||pushDomainArr.length==0)
		{
			Tools.showLog("ZZDomainControl-getPushDomainArr", "pushDomainArr==null||pushDomainArr.length==0");
			pushDomainArr = PUSH_DOMAIN;
		}
		return pushDomainArr;
	}
	
	/**
	 * 更新配置域名列表
	 * @param context
	 * @param list
	 */
	private void updateConfigeDomainArr(Context context,ArrayList<String> list) {
		if(list!=null&&list.size()>0)
		{
			configeDomainArr = Tools.arrayListToArr(list);
			if(context!=null)
			{
				String configeDomainArrStr = Tools.mergeStr(list, split);
				Tools.showLog("ZZDomainControl-updateConfigeDomainArr", "configeDomainArrStr="+configeDomainArrStr);
				if(configeDomainArrStr!=null&&configeDomainArrStr.length()>0)
				{
					Tools.saveData(context, DB_NAME, "configeDomainArrStr", configeDomainArrStr);
				}
			}
		}
	}
	
	/**
	 * 更新YY域名列表
	 * @param context
	 * @param list
	 */
	private void updateYYDomainArr(Context context,ArrayList<String> list) {
		if(list!=null&&list.size()>0)
		{
			String domain = list.get(0);
			if(pingDomain(context,domain))
			{
				updateYYDomain(context,domain);
			}
			yyDomainArr = Tools.arrayListToArr(list);
			if(context!=null)
			{
				String yyDomainArrStr = Tools.mergeStr(list, split);
				Tools.showLog("ZZDomainControl-updateYYDomainArr", "yyDomainArrStr="+yyDomainArrStr);
				if(yyDomainArrStr!=null&&yyDomainArrStr.length()>0)
				{
					Tools.saveData(context, DB_NAME, "yyDomainArrStr", yyDomainArrStr);
				}
			}
		}
	}
	
	/**
	 * 更新PUSH域名列表
	 * @param context
	 * @param list
	 */
	private void updatePushDomainArr(Context context,ArrayList<String> list) {
		if(list!=null&&list.size()>0)
		{
			String domain = list.get(0);
			if(pingDomain(context,domain))
			{
				updatePushDomain(context,domain);
			}
			pushDomainArr = Tools.arrayListToArr(list);
			if(context!=null)
			{
				String pushDomainArrStr = Tools.mergeStr(list, split);
				Tools.showLog("ZZDomainControl-updatePushDomainArr", "pushDomainArrStr="+pushDomainArrStr);
				if(pushDomainArrStr!=null&&pushDomainArrStr.length()>0)
				{
					Tools.saveData(context, DB_NAME, "pushDomainArrStr", pushDomainArrStr);
				}
			}
		}
	}
	
	/**
	 * 线程下载新的域名列表
	 */
	private Thread downloadNewDomainThread;
	public void downloadNewDomain(final Context context) {
		if(context==null)
		{
			return;
		}
		if(downloadNewDomainThread==null)
		{
			String lastDownloadDomainTime = Tools.getSaveData(context, DB_NAME, "lastDownloadDomainTime", null);
			if(lastDownloadDomainTime!=null&&lastDownloadDomainTime.length()>0)
			{
				try {
					long lastTime = Long.parseLong(lastDownloadDomainTime);
					long currentTime = System.currentTimeMillis();
					if(currentTime-lastTime<3*24*60*60*1000L) {//每3天更新一次域名列表    //10000L
						if(currentTime<lastTime)//时间被修改到过去，重新校准时间
						{
							Tools.showLog("ZZDomainControl-downloadNewDomain", "时间被篡改到过去-上次域名配置拉取时间重新置位");
							Tools.saveData(context, DB_NAME, "lastDownloadDomainTime", currentTime+"");
						}
						else
						{
							Tools.showLog("ZZDomainControl-downloadNewDomain", "3天内已经更新过一次配置");
						}
						return;
					}
				} catch(NumberFormatException e)
				{
					Tools.saveData(context, DB_NAME, "lastDownloadDomainTime", "");
					return;
				}
			}
			String lastCheckDownloadDomainEndTime = Tools.getSaveData(context, DB_NAME, "lastCheckDownloadDomainEndTime", null);
			Tools.showLog("ZZDomainControl-downloadNewDomain", "lastCheckDownloadDomainEndTime="+lastCheckDownloadDomainEndTime);
			if(lastCheckDownloadDomainEndTime!=null&&lastCheckDownloadDomainEndTime.length()>0)
			{
				try {
					long lastCheckTime = Long.parseLong(lastCheckDownloadDomainEndTime);
					long currentTime = System.currentTimeMillis();
					if(currentTime-lastCheckTime<10*60*1000L) {//每10分钟检测一次域名列表是否可用   //10000L
						Tools.showLog("ZZDomainControl-downloadNewDomain", "10分钟内已经检测下载过一次");
						if(currentTime<lastCheckTime)//时间被修改到过去，重新校准时间
						{
							Tools.showLog("ZZDomainControl-downloadNewDomain", "时间被篡改到过去-上次检测配置更新的时间重新置位");
							Tools.saveData(context, DB_NAME, "lastCheckDownloadDomainEndTime", currentTime+"");
						}
						else
						{
							Tools.showLog("ZZDomainControl-downloadNewDomain", "10分钟内已经检测过一次配置更新");
						}
						return;
					}
				} catch(NumberFormatException e) {
					Tools.showLog("ZZDomainControl-downloadNewDomain", "NumberFormatException："+e.getMessage());
					Tools.saveData(context, DB_NAME, "lastCheckDownloadDomainEndTime", "");
				}
			}
			downloadNewDomainThread = new Thread("ZZDomainControl-downloadNewDomain") {
				public void run() {
					String[] configeDomainArr = getConfigeDomainArr(context);
					String configeUrl;
					String domainListStr;
					for(int i=0;i<configeDomainArr.length;i++) {
						configeUrl = configeDomainArr[i];
						if(configeUrl!=null&&configeUrl.length()>0)
						{
							if(!SystemInfo.isNetworkAvailable(context))
							{
								Tools.showLog("ZZDomainControl-downloadNewDomain", "NetWork is not Available!");
								break;
							}
							domainListStr = null;
							String requestUpdate = "";
							String lastUpdateDomainTime = Tools.getSaveData(context, DB_NAME, "lastUpdateDomainTime", null);
							if(lastUpdateDomainTime!=null&&lastUpdateDomainTime.length()>0)
							{
								try {
									long currentTime = System.currentTimeMillis();
									long updateTime = Long.parseLong(lastUpdateDomainTime);
									if(currentTime-updateTime<15*24*60*60*1000L)
									{
										if(currentTime<updateTime)
										{
											Tools.saveData(context, DB_NAME, "lastUpdateDomainTime", currentTime+"");
										}
									}
									else//一组域名列表使用时间超过15天，则请求强制更新域名列表
									{
										requestUpdate = "&requestUpdate=true";
									}
								} catch(NumberFormatException e) {
									Tools.showLog("ZZDomainControl-downloadNewDomain", "NumberFormatException："+e.getMessage());
									Tools.saveData(context, DB_NAME, "lastUpdateDomainTime", "");
								}
							}
							else
							{
								//requestUpdate = "&requestUpdate=true";
							}
							if(httpRequest==null)
							{
								httpRequest = new HttpRequest(context);
							}
							httpRequest.setRedirecting(true);
							domainListStr = httpRequest.getUrl(configeUrl+CONFIGE_RELATIVE_URL+"?"+SystemInfo.requestUploadGetParam(context)+requestUpdate, null);
							Tools.showLog("ZZDomainControl-downloadNewDomain", "domainListStr="+domainListStr);
							if(domainListStr!=null&&domainListStr.length()>0)
							{
								String domainListStrHash = Tools.getSaveData(context, DB_NAME, "domainListStrHash", null);
								String hash = Tools.getStringHashValue(domainListStr)+"-"+domainListStr.length();
								if(domainListStrHash==null||domainListStrHash.length()==0||!domainListStrHash.equals(hash))
								{
									parseDomainListJson(context,domainListStr);
									Tools.saveData(context, DB_NAME, "domainListStrHash", hash);
									Tools.saveData(context, DB_NAME, "lastUpdateDomainTime", System.currentTimeMillis()+"");//记录域名列表上次更新的时间
								}
								Tools.saveData(context, DB_NAME, "lastDownloadDomainTime", System.currentTimeMillis()+"");
								break;
							}
						}
					}
					Tools.saveData(context, DB_NAME, "lastCheckDownloadDomainEndTime", System.currentTimeMillis()+"");
					show(context,"end downloadNewDomain");
					downloadNewDomainThread=null;
					unInit();
				}
			};
			try {
				downloadNewDomainThread.start();
			} catch(NullPointerException e) {
				
			} catch(IllegalThreadStateException e) {
				
			}
		}
	}
	
	/**
	 * 解析域名列表数据
	 * @param adJsonString
	 * @return
	 */
	private void parseDomainListJson(Context context,String domainListStr) {
		if(context==null)
		{
			return;
		}
		Tools.showLog("ZZDomainControl-parseDomainListJson", "domainListStr="+domainListStr);
		try {
			JSONObject adJson = new JSONObject(domainListStr);
			String configs = adJson.optString("configs");
			if(configs!=null&&configs.toLowerCase().equals("update"))//判断域名是否有更新
			{
				JSONObject tempJson;
				String tempDomain;
				JSONArray configArr = adJson.optJSONArray("config");//获取配置域名列表
				if(configArr!=null&&configArr.length()>0)
				{
					ArrayList<String> configList = new ArrayList<String>();
					for(int i=0;i<configArr.length();i++) {
						tempJson = configArr.optJSONObject(i);
						if(tempJson!=null)
						{
							tempDomain = tempJson.optString("domain");
							if(tempDomain!=null&&tempDomain.length()>0)
							{
								if(!tempDomain.toLowerCase().startsWith(HTTP))
								{
									tempDomain = HTTP_HEAD+tempDomain;
								}
								configList.add(tempDomain);
							}
						}
					}
					updateConfigeDomainArr(context,configList);
				}
				else
				{
					Tools.showLog("ZZDomainControl-parseDomainListJson", "configArr==null||configArr.length()==0");
				}
				
				JSONArray yyArr = adJson.optJSONArray("yy");//获取YY域名列表
				if(yyArr!=null&&yyArr.length()>0)
				{
					ArrayList<String> yyList = new ArrayList<String>();
					for(int i=0;i<yyArr.length();i++) {
						tempJson = yyArr.optJSONObject(i);
						if(tempJson!=null)
						{
							tempDomain = tempJson.optString("domain");
							if(tempDomain!=null&&tempDomain.length()>0)
							{
								if(!tempDomain.toLowerCase().startsWith(HTTP))
								{
									tempDomain = HTTP_HEAD+tempDomain;
								}
								yyList.add(tempDomain);
							}
						}
					}
					updateYYDomainArr(context,yyList);
				}
				else
				{
					Tools.showLog("ZZDomainControl-parseDomainListJson", "yyArr==null||yyArr.length()==0");
				}
				
				JSONArray pushArr = adJson.optJSONArray("push");//获取PUSH域名列表
				if(pushArr!=null&&pushArr.length()>0)
				{
					ArrayList<String> pushList = new ArrayList<String>();
					for(int i=0;i<pushArr.length();i++) {
						tempJson = pushArr.optJSONObject(i);
						if(tempJson!=null)
						{
							tempDomain = tempJson.optString("domain");
							if(tempDomain!=null&&tempDomain.length()>0)
							{
								if(!tempDomain.toLowerCase().startsWith(HTTP))
								{
									tempDomain = HTTP_HEAD+tempDomain;
								}
								pushList.add(tempDomain);
							}
						}
					}
					updatePushDomainArr(context,pushList);
				}
				else
				{
					Tools.showLog("ZZDomainControl-parseDomainListJson", "pushArr==null||pushArr.length()==0");
				}
			}
		} catch (JSONException e) {
			Tools.showLog("ZZDomainControl-JSONException", "JSONException:"+e.getMessage());
		}
	}
	
	private Thread checkDomainAvailableThread;
	private int checkType;
	/**
	 * 启动线程检测域名列表是否可用
	 * @param context
	 */
	public void checkDomainAvailable(final Context context) {
		if(context==null)
		{
			return;
		}
		if(checkDomainAvailableThread==null) {
			checkType = 0;
			String currentDate = Tools.getCurrentDate(null, null);
			String yyDomainOKDate = Tools.getSaveData(context, DB_NAME, "domain"+DOMAIN_TYPE_YY+"-OKDate", null);
			String checkDomainDate = Tools.getSaveData(context, DB_NAME, "checkDomainDate", null);
			String yyDomainCloseDate = Tools.getSaveData(context, DB_NAME, "domain"+DOMAIN_TYPE_YY+"-CloseDate", null);
			Tools.showLog("ZZDomainControl-checkDomainAvailable", "currentDate="+currentDate+" yyDomainOKDate="+yyDomainOKDate+" checkDomainDate="+checkDomainDate+" yyDomainCloseDate="+yyDomainCloseDate);
			if(yyDomainOKDate!=null&&yyDomainOKDate.length()>0)
			{
				if(yyDomainCloseDate==null||yyDomainCloseDate.length()==0||!yyDomainCloseDate.equals(currentDate))//今天没有关闭YY PING功能
				{
					if(!yyDomainOKDate.equals(currentDate))//今天还没有PING通过
					{
						Tools.showLog("ZZDomainControl-checkDomainAvailable", "需要检测YY域名的可用性！");
						checkType+=DOMAIN_TYPE_YY;
						if(checkDomainDate==null||checkDomainDate.length()==0||!checkDomainDate.equals(currentDate))//今天第一次检测域名是否可用，清零测试失败数据。
						{
							Tools.showLog("ZZDomainControl-checkDomainAvailable", "新的一天检测开始，YY域名之前的计数清零！");
							clearDomainList(context,DOMAIN_TYPE_YY,getYYDomainArr(context));
						}
					}
					else
					{
						Tools.showLog("ZZDomainControl-checkDomainAvailable", "今天已经成功检测过一次YY域名可用！");
					}
				}
				else
				{
					Tools.showLog("ZZDomainControl-checkDomainAvailable", "今天的yyDomain检测功能已经被关闭！");
				}
			}
			else
			{
				if(yyDomainCloseDate==null||yyDomainCloseDate.length()==0||!yyDomainCloseDate.equals(currentDate))//今天没有关闭YY PING功能
				{
					Tools.showLog("ZZDomainControl-checkDomainAvailable", "需要检测YY域名的可用性！");
					checkType+=DOMAIN_TYPE_YY;
				}
				else
				{
					Tools.showLog("ZZDomainControl-checkDomainAvailable", "今天的yyDomain检测功能已经被关闭！");
				}
			}
			
			String pushDomainOKDate = Tools.getSaveData(context, DB_NAME, "domain"+DOMAIN_TYPE_PUSH+"-OKDate", null);
			String pushDomainCloseDate = Tools.getSaveData(context, DB_NAME, "domain"+DOMAIN_TYPE_PUSH+"-CloseDate", null);
			Tools.showLog("ZZDomainControl-checkDomainAvailable", "pushDomainOKDate="+pushDomainOKDate+" pushDomainCloseDate="+pushDomainCloseDate);
			if(pushDomainOKDate!=null&&pushDomainOKDate.length()>0)
			{
				if(pushDomainCloseDate==null||pushDomainCloseDate.length()==0||!pushDomainCloseDate.equals(currentDate))//今天没有关闭PUSH PING功能
				{
					if(!pushDomainOKDate.equals(currentDate))//今天还没有PING通过
					{
						Tools.showLog("ZZDomainControl-checkDomainAvailable", "需要检测PUSH域名的可用性！");
						checkType+=DOMAIN_TYPE_PUSH;
						if(checkDomainDate==null||checkDomainDate.length()==0||!checkDomainDate.equals(currentDate))
						{
							Tools.showLog("ZZDomainControl-checkDomainAvailable", "新的一天检测开始，PUSH域名之前的计数清零！");
							clearDomainList(context,DOMAIN_TYPE_PUSH,getPushDomainArr(context));
						}
					}
					else
					{
						Tools.showLog("ZZDomainControl-checkDomainAvailable", "今天已经成功检测过一次PUSH域名可用！");
					}
				}
				else
				{
					Tools.showLog("ZZDomainControl-checkDomainAvailable", "今天的pushDomain检测功能已经被关闭！");
				}
			}
			else
			{
				if(pushDomainCloseDate==null||pushDomainCloseDate.length()==0||!pushDomainCloseDate.equals(currentDate))//今天没有关闭PUSH PING功能
				{
					Tools.showLog("ZZDomainControl-checkDomainAvailable", "需要检测YY域名的可用性！");
					checkType+=DOMAIN_TYPE_PUSH;
				}
				else
				{
					Tools.showLog("ZZDomainControl-checkDomainAvailable", "今天的pushDomain检测功能已经被关闭！");
				}
			}
			Tools.showLog("ZZDomainControl-checkDomainAvailable", "checkType="+checkType);
			if(checkType>0)
			{
				Tools.saveData(context, DB_NAME, "checkDomainDate", currentDate);
				String lastCheckDomainEndTime = Tools.getSaveData(context, DB_NAME, "lastCheckDomainEndTime", null);
				Tools.showLog("ZZDomainControl-checkDomainAvailable", "lastCheckDomainEndTime="+lastCheckDomainEndTime);
				if(lastCheckDomainEndTime!=null&&lastCheckDomainEndTime.length()>0)
				{
					try {
						long lastCheckTime = Long.parseLong(lastCheckDomainEndTime);
						long currentTime = System.currentTimeMillis();
						if(currentTime-lastCheckTime<10*60*1000L) {//每10分钟检测一次域名列表是否可用    //10000L
							Tools.showLog("ZZDomainControl-checkDomainAvailable", "10分钟内已经检测过一次");
							if(currentTime<lastCheckTime)//时间被修改到过去，重新校准时间
							{
								Tools.showLog("ZZDomainControl-checkDomainAvailable", "时间被篡改到过去");
								Tools.saveData(context, DB_NAME, "lastCheckDomainEndTime", currentTime+"");
							}
							return;
						}
					} catch(NumberFormatException e) {
						Tools.showLog("ZZDomainControl-checkDomainAvailable", "NumberFormatException："+e.getMessage());
						Tools.saveData(context, DB_NAME, "lastCheckDomainEndTime", "");
					}
				}
				checkDomainAvailableThread = new Thread("ZZDomainControl-checkDomainAvailable") {
					public void run() {
						if((checkType&DOMAIN_TYPE_YY)>0)
						{
							Tools.showLog("ZZDomainControl-checkDomainAvailable", "checkYYDomainList begin");
							checkDomainList(context,DOMAIN_TYPE_YY,getYYDomainArr(context));
							Tools.showLog("ZZDomainControl-checkDomainAvailable", "checkYYDomainList end");
						}
						if((checkType&DOMAIN_TYPE_PUSH)>0)
						{
							Tools.showLog("ZZDomainControl-checkDomainAvailable", "checkPushDomainList begin");
							checkDomainList(context,DOMAIN_TYPE_PUSH,getPushDomainArr(context));
							Tools.showLog("ZZDomainControl-checkDomainAvailable", "checkPushDomainList end");
						}
						Tools.saveData(context, DB_NAME, "lastCheckDomainEndTime", System.currentTimeMillis()+"");
						show(context,"end checkDomainAvailable");
						checkDomainAvailableThread=null;
						unInit();
					}
				};
				try {
					checkDomainAvailableThread.start();
				} catch(NullPointerException e) {
					
				} catch(IllegalThreadStateException e) {
					
				}
			}
			else
			{
				Tools.showLog("ZZDomainControl-checkDomainAvailable", "今天已经无需再检测YY或PUSH域名的可用性！");
			}
		}
	}
	
	/**
	 * 清零域名列表的当日PING失败次数
	 * @param context
	 * @param type
	 * @param domainArr
	 */
	private void clearDomainList(Context context,int type,String[] domainArr) {
		if(context==null||domainArr==null||domainArr.length==0)
		{
			return;
		}
		Tools.showLog("ZZDomainControl-clearDomainList", "type="+type);
		String pingUrl;
		int hash;
		for(int i=0;i<domainArr.length;i++) {
			pingUrl = domainArr[i];
			if(pingUrl!=null&&pingUrl.length()>0)
			{
				hash = Tools.getStringHashValue(pingUrl);
				Tools.saveData(context, DB_NAME, "domain"+type+"-"+hash+"-"+pingUrl.length(), 0);
			}
		}
	}
	
	/**
	 * 检测当前的域名列表是否可用
	 * @param context
	 * @param type
	 * @param domainArr
	 */
	private void checkDomainList(Context context,int type,String[] domainArr) {
		if(context==null||domainArr==null||domainArr.length==0)
		{
			return;
		}
		String pingUrl;
		int count;
		int hash;
		String currentDate = Tools.getCurrentDate(null, null);
		for(int i=0;i<domainArr.length;i++) {
			pingUrl = domainArr[i];
			if(pingUrl!=null&&pingUrl.length()>0)
			{
				if(!SystemInfo.isNetworkAvailable(context))
				{
					Tools.showLog("ZZDomainControl-checkDomainList", "NetWork is not Available!");
					break;
				}
				hash = Tools.getStringHashValue(pingUrl);
				count = Tools.getSaveData(context, DB_NAME, "domain"+type+"-"+hash+"-"+pingUrl.length(), 0);//获取当前域名当日PING失败次数
				Tools.showLog("ZZDomainControl-checkDomainList", "pingUrl="+pingUrl+" get fail count="+count);
				if(count>=3)
				{
					if(i<domainArr.length-1)//当前域名PING失败超过3次，PING次级域名
					{
						Tools.showLog("ZZDomainControl-checkDomainList", "next domain");
						continue;
					}
					else//所有的域名均已经PING过3次以上，今天不再进行PING操作，第二日再重新PING。
					{
						Tools.showLog("ZZDomainControl-checkDomainList", "close check domain today");
						Tools.saveData(context, DB_NAME, "domain"+type+"-CloseDate", currentDate);//所有域名均3次以上PING不通，关闭当日PING操作。
						break;
					}
				}
				if(pingDomain(context,pingUrl))//正常PING通
				{
					if(type==DOMAIN_TYPE_YY)
					{
						updateYYDomain(context,pingUrl);//更新YY默认域名
					}
					else if(type==DOMAIN_TYPE_PUSH)
					{
						updatePushDomain(context,pingUrl);//更新PUSH默认域名
					}
					else
					{
						
					}
					Tools.saveData(context, DB_NAME, "domain"+type+"-OKDate", currentDate);//记录当日域名PING成功
					Tools.saveData(context, DB_NAME, "domain"+type+"-CloseDate", "");//记录当日域名非关闭状态
					clearDomainList(context,type,domainArr);//清零当日失败次数
				}
				else
				{
					count++;
					Tools.showLog("ZZDomainControl-checkDomainList", "pingUrl="+pingUrl+" save fail count="+count);
					Tools.saveData(context, DB_NAME, "domain"+type+"-"+hash+"-"+pingUrl.length(), count);//记录PING失败的次数
					if(count>=3&&i==domainArr.length-1)//当前域名PING失败超过3次，PING次级域名
					{
						Tools.showLog("ZZDomainControl-checkDomainList", "close check domain today end");
						Tools.saveData(context, DB_NAME, "domain"+type+"-CloseDate", currentDate);//所有域名均3次以上PING不通，关闭当日PING操作。
					}
				}
				break;
			}
		}
	}
	
	/**
	 * 检测当前域名是否可用
	 * @param context
	 * @param domain
	 * @return
	 */
	private boolean pingDomain(Context context,String domain) {
		if(context==null||!SystemInfo.isNetworkAvailable(context))
		{
			Tools.showLog("ZZDomainControl-pingDomain", "NetWork is not Available!");
			return false;
		}
		if(httpRequest==null)
		{
			httpRequest = new HttpRequest(context);
		}
		httpRequest.setRedirecting(false);
		HttpResponse response = httpRequest.getUrlHeaderList(domain+PING_RELATIVE_URL+"?"+SystemInfo.requestUploadGetParam(context), null);
		if(httpRequest.isHttpResponseAvailable(response))
		{
			return true;
		}
		return false;
	}
	
	/**
	 * 资源卸载-只卸载网络请求相关部分
	 */
	private void unInit() {
		if(downloadNewDomainThread==null&&checkDomainAvailableThread==null)
		{
			if(httpRequest!=null)
			{
				httpRequest.unInit();
				httpRequest = null;
			}
		}
	}

}
